package com.dcm.live.controller;

import com.dcm.common.annotation.EnableResponseBodyAdvice;
import com.dcm.live.entity.LiveRoomEntity;
import com.dcm.live.service.FocusService;
import com.dcm.live.service.LikedService;
import com.dcm.live.service.LiveRoomService;
import com.dcm.live.vo.LiveVo;
import com.dcm.live.vo.OnlineVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 直播记录
 *
 * @author : [pengrongsheng]and[zhouyuhan]
 * @createTime : [2021/3/28]
 */
@RestController
@EnableResponseBodyAdvice
@RequestMapping("/liveRoom")
public class LiveRoomController {
    @Autowired
    private LiveRoomService liveRoomService;

    @ApiOperation("查询所有直播间")
    @GetMapping("/findAll")
    public List<LiveRoomEntity> findAll() {
        return liveRoomService.findAll();
    }

    @ApiOperation("根据id查询直播间")
    @GetMapping("/findRoomById")
    public LiveRoomEntity findRoomById(@RequestParam(value = "id", required = false) Long id) {
        return liveRoomService.findRoomById(id);
    }

    @ApiOperation("根据状态查询直播间")
    @GetMapping("/findRoomByStatus")
    public List<LiveRoomEntity> findRoomByStatus(@RequestParam(value = "status", required = false) Integer status) {
        return liveRoomService.findRoomByStatus(status);
    }

    @ApiOperation("添加")
    @PostMapping("/addLiveRoom")
    public void addLiveRoom(@RequestBody LiveRoomEntity liveRoomEntity) {
        liveRoomService.addRoom(liveRoomEntity);
    }

    @ApiOperation("修改")
    @PutMapping("/updateLiveRoom")
    public void updateLiveRoom(@RequestBody LiveRoomEntity liveRoomEntity) {
        liveRoomService.updateRoom(liveRoomEntity);
    }

    @ApiOperation("页面推荐")
    @GetMapping("/findRoomByKeyWords")
    public List<LiveRoomEntity> findRoomByKeyWords(@RequestParam(value = "keyWords", required = false) String keyWords) {
        return liveRoomService.findRoomByKeyWords(keyWords);
    }

    @ApiOperation("显示直播人、在线人数、在线人头像只展示10个")
    @GetMapping("/findOnline")
    public List<OnlineVo> findOnline(@RequestParam(value = "uid") Long uid) {
        return liveRoomService.findOnline(uid);
    }

    @ApiOperation("分页")
    @GetMapping("/findPage")
    public PageInfo<LiveRoomEntity> findPage(@RequestParam(value = "currentPage", required = false) Integer currentPage,
                                             @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        return liveRoomService.findPage(currentPage, pageSize);
    }

    @Autowired
    private LikedService likedService;
    @Autowired
    private FocusService focusService;

    @Scheduled(cron = "0 0/2 * * * ?")
    public void loadRedis2db() {
        likedService.transLikedCountFromRedis2db();
        likedService.transLikedFromRedis2db();
        focusService.transFocusCountFromRedis2db();
        focusService.transFromRedis2db();
    }

    @ApiOperation("点赞")
    @GetMapping("/saveLike")
    public void saveLike(@RequestParam(value = "uid")Long uid,
                         @RequestParam(value = "pid")Long pid) {
        likedService.saveLiked2Redis(uid, pid);
    }

    @ApiOperation("取消点赞")
    @GetMapping("/unlike")
    public void unlike(@RequestParam(value = "uid")Long uid,
                         @RequestParam(value = "pid")Long pid) {
        likedService.unLikedFromRedis(uid, pid);
    }

    @ApiOperation("关注")
    @GetMapping("focusOn")
    public void focusOn(@RequestParam(value = "uid") Long uid,
                        @RequestParam(value = "pid") Long pid) {
        focusService.saveFocus2Redis(uid,pid);
    }
    @ApiOperation("取消关注")
    @GetMapping("blur")
    public void blur(@RequestParam(value = "uid") Long uid,
                        @RequestParam(value = "pid") Long pid) {
        focusService.blurFromRedis(uid,pid);
    }

    @ApiOperation("查看当前观看人数、当前关注人数、当前点赞人数、当前下单人数")
    @GetMapping("/findLive")
    public List<LiveVo>findLive(@RequestParam(value = "id",required = false)Long id){
        return liveRoomService.findLive(id);
    }

    @ApiOperation("状态：可关闭1、开启0")
    @PutMapping("/liveStatus")
    public void liveStatus(@RequestParam(value = "id",required = false)Long id,
                           @RequestParam(value = "status",required = false)Integer status){
        liveRoomService.liveStatus(id,status);
    }

    @ApiOperation("已结束状态：删除")
    @DeleteMapping("/liveDelete")
    public void liveDelete(@RequestParam(value = "id",required = false)Long id,
                           @RequestParam(value = "status",required = false)Integer status){
        liveRoomService.liveDelete(id,status);
    }
}
